package plugin;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.RenderingHints.Key;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;

import javax.swing.ImageIcon;

public class StopTimeIcon {

	private RenderingHints hints;

	protected StopTimeIcon() {
		HashMap<Key, Object> map = new HashMap<Key, Object>();
		map.put(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);

		map.put(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);

		map.put(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		hints = new RenderingHints(map);
	}

	protected ImageIcon getStopImage(int minutes) {
		return new ImageIcon(addTimeToImage(minutes, new ImageIcon(
				"./src/plugin/images/icon_halt.gif")));
	}
	
	protected ImageIcon getStopForceImage(int minutes) {		
		return new ImageIcon(addTimeToImage(minutes, new ImageIcon(
				"./src/plugin/images/icon_vorzeitig.gif")));
	}

	protected ImageIcon getStopIfNeededImage(int minutes) {
		return new ImageIcon(addTimeToImage(minutes, new ImageIcon(
				"./src/plugin/images/icon_bedarfshalt.gif")));
	}
	
	protected ImageIcon getStopReverseImage(int minutes) {
		return new ImageIcon(addTimeToImage(minutes, new ImageIcon(
				"./src/plugin/images/icon_halt_umdrehen.gif")));
	}

	protected BufferedImage addTimeToImage(int minutes, ImageIcon source) {
		String string = minutes + "";
		if (string.equals("0")) string ="K";
		BufferedImage img = toBufferedImage(source.getImage(), false);
		BufferedImage aimg = new BufferedImage(img.getWidth(), img.getHeight(),
				BufferedImage.TRANSLUCENT);
		Graphics2D g = aimg.createGraphics();
		g.addRenderingHints(hints);
		g.drawImage(img, null, 0, 0);

		// AlphaComposite alpha = AlphaComposite.getInstance(
		// AlphaComposite.SRC_ATOP, this.transparency);
		// g.setComposite(alpha);
		g.setColor(Color.BLACK);
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g.setFont(new Font("Verdana", Font.BOLD, 9));
		g.translate(aimg.getWidth() / 2, aimg.getHeight() / 2);
		// g.rotate(Math.toRadians(angle));
		FontMetrics fontMetrics = g.getFontMetrics();
		Rectangle2D rect = fontMetrics.getStringBounds(string, g);

		int relativePosX = 5;
		int relativePosY = 9;
		g.drawString(string, ((0 - (int) rect.getWidth()) / 2) + relativePosX,
				((0 - (int) rect.getHeight()) / 2) + relativePosY);
		g.dispose();
		return aimg;
	}

	protected BufferedImage toBufferedImage(Image image, boolean hasAlpha) {
		if (image instanceof BufferedImage) {
			return (BufferedImage) image;
		}
		BufferedImage bimage = null;
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		try {
			int transparency = hasAlpha ? Transparency.BITMASK
					: Transparency.OPAQUE;
			GraphicsDevice gs = ge.getDefaultScreenDevice();
			GraphicsConfiguration gc = gs.getDefaultConfiguration();
			bimage = gc.createCompatibleImage(image.getWidth(null),
					image.getHeight(null), transparency);
		} catch (HeadlessException e) {
		}

		if (bimage == null) {

			int type = BufferedImage.TYPE_INT_RGB;
			if (hasAlpha) {
				type = BufferedImage.TYPE_INT_ARGB;
			}
			bimage = new BufferedImage(image.getWidth(null),
					image.getHeight(null), type);
		}

		Graphics g = bimage.getGraphics();

		// Paint the image onto the buffered image
		g.drawImage(image, 0, 0, null);
		g.dispose();

		return bimage;
	}

	



}
